package org.timeismoney.payment.web.action.teacher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.timeismoney.payment.common.Md5Util;
import org.timeismoney.payment.common.PageBean;
import org.timeismoney.payment.model.Teacher;
import org.timeismoney.payment.service.TeacherService;
import org.timeismoney.payment.web.WebUtils;
import org.timeismoney.payment.web.action.BaseAction;
import org.timeismoney.payment.web.form.TeacherForm;

public class AdminTeacherAction extends BaseAction {
	private static Logger	log	= Logger.getLogger(AdminTeacherAction.class);
	private TeacherService	service;

	public void setService(TeacherService service) {
		this.service = service;
	}

	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// rows=10&page=1
		// sidx schoolYearNo
		// sord desc
		String rows = request.getParameter("rows");
		String page = request.getParameter("page");
		String sortCol = request.getParameter("sidx");
		String sortD = request.getParameter("sord");
		int rowsNo = 10;
		try {
			rowsNo = Integer.valueOf(rows);
		} catch (RuntimeException e) {
		}
		int pageNo = 1;
		try {
			pageNo = Integer.valueOf(page);
		} catch (RuntimeException e) {
		}
		PageBean<Teacher> pageBean = new PageBean<Teacher>(pageNo, rowsNo,
				sortCol, sortD);
		service.findByPageBean(pageBean);
		renderJson(response, WebUtils.generateJSONByPageBean(pageBean));
		return null;
	}

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		try {
			TeacherForm teacherForm = (TeacherForm) form;
			Teacher teacher = new Teacher();
			buildModel(teacherForm, teacher);
			teacher.setPwd(Md5Util.MD5(teacher.getPwd()));
			service.save(teacher);
		} catch (Exception e) {
			log.error(e.getMessage());
			renderJson(
					response,
					WebUtils.generateErrorJSON(TEXT_ERROR_MSG_ADD + " "
							+ e.getMessage()));
		}
		return null;
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		try {
			TeacherForm teacherForm = (TeacherForm) form;
			Teacher teacher = new Teacher();
			buildModel(teacherForm, teacher);
			teacher.setPwd(Md5Util.MD5(teacher.getPwd()));
			service.update(teacher);
		} catch (Exception e) {
			log.error(e.getMessage());
			renderJson(
					response,
					WebUtils.generateErrorJSON(TEXT_ERROR_MSG_EDIT + " "
							+ e.getMessage()));
		}
		return null;
	}

	public ActionForward del(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		try {
			TeacherForm teacherForm = (TeacherForm) form;
			Teacher teacher = service.get(teacherForm.getTeacherNo());
			service.delete(teacher);
		} catch (Exception e) {
			log.error(e.getMessage());
			renderJson(
					response,
					WebUtils.generateErrorJSON(TEXT_ERROR_MSG_DEL + " "
							+ e.getMessage()));
		}
		return null;
	}

}
